import type { Ref } from 'vue';
// import type { ActionItem } from '/@/components/Table';
import { unref } from 'vue';
import { ActionModel, ActionItemModel, Callback, DelBatchType } from '../types/index';
import { BTN_EVENT_TYPE } from '/@/views/config-center/model-design/model';

// import { useMessage } from '/@/hooks/web/useMessage';
import { ModelIds } from '/@/api/config-center/model-design';
import {
  customRowFnForApi,
  commonDel,
  getRowKeys,
  commonFlowDel,
  commonDownLoad,
} from './functionSet';

// const { createMessage } = useMessage();
// 表格按钮根据行的某个属性隐藏
function hideButton(visibleExpression, record) {
  if (!visibleExpression) {
    return false;
  }
  const fn = new Function(
    'record',
    `return (function(context,args,defHttp){${visibleExpression}})(record)`,
  );

  return fn(record);
}

export function useTableBtns() {}

// 表格内部操作
export function useActions(
  btns: Ref<Indexable[]>,
  action: ActionModel,
  callbacks: Callback[],
): any[] {
  const { primaryKey = '', record, moduleId, tableModelId } = action;
  const ids: ModelIds = { moduleId, modelId: tableModelId };
  const params: Indexable = getRowKeys(primaryKey, record);
  const [
    reload,
    setModalPropsFn,
    openModalFn,
    handleFlowComFn,
    importFile,
    handleCustomComponent,
    tableFn,
  ] = callbacks;
  const ret = unref(btns).map((e) => {
    const isHideButton = hideButton(e.visibleExpression, record);

    e.ifShow = !isHideButton;
    e.label = e.btnText || e.label;
    // primaryKey
    switch (e.eventType) {
      case BTN_EVENT_TYPE.ADD:
        e.onClick = async function () {
          const item = {
            ...e,
            moduleId,
            tableModelId,
            params,
            record,
          };
          setModalPropsFn && setModalPropsFn(item);
          openModalFn && openModalFn(item);
        };
        return e;
      case BTN_EVENT_TYPE.DELETE:
        return commonDel({ source: e, ids, params }, reload);
      case BTN_EVENT_TYPE.EDIT:
        e.onClick = async function () {
          const item = {
            ...e,
            isUpdate: true,
            moduleId,
            tableModelId,
            params,
            record,
          };
          setModalPropsFn && setModalPropsFn(item);
          openModalFn && openModalFn(item);
        };
        return e;

      case BTN_EVENT_TYPE.CUSTOM:
        e.onClick = customRowFnForApi.bind(
          null,
          e,
          {
            record,
            ids,
            primaryKeys: params,
          } as ActionItemModel,
          { reload },
        );
        return e;
      case BTN_EVENT_TYPE.DETAIL:
        e.onClick = async function () {
          const item = {
            ...e,
            componentName: record.componentName || e.componentName,
            modelClassify: record.modelClassify || e.modelClassify,
            dataModelId: record.dataModelId || e.dataModelId,
            modelId: record.dataModelId || e.dataModelId,
            parentModel: { moduleId, tableModelId },
            params,
            record,
            hanldRow: true,
          };
          handleCustomComponent && handleCustomComponent({ ...item, tabFn: tableFn() });
        };
        return e;
      case BTN_EVENT_TYPE.FLOWDELETE:
        // 流程草稿单个删除
        return commonFlowDel(DelBatchType.FLOWDELETE, { source: e, record }, reload);
      case BTN_EVENT_TYPE.FLOWHANDROUNDDELETE:
        // 流程传阅单个删除
        return commonFlowDel(DelBatchType.FLOWHANDROUNDDELETE, { source: e, record }, reload);
      case BTN_EVENT_TYPE.FLOWADD:
        // 流程新建,办理
        e.onClick = async function () {
          const item = {
            ...e,
            moduleId,
            tableModelId,
            params,
            record,
          };
          openModalFn && openModalFn(item);
        };
        return e;
      case BTN_EVENT_TYPE.FLOWCHCEK:
        // 流程详情
        e.onClick = async function () {
          const item = {
            ...e,
            moduleId,
            tableModelId,
            params,
            isCheck: true,
            record,
          };
          openModalFn && openModalFn(item);
        };
        return e;
      case BTN_EVENT_TYPE.FLOWCONSULT:
        // 流程征询办理
        const { ReadComment } = record;

        e.disabled = !!ReadComment;
        e.onClick = async function () {
          const item = {
            ...e,
            moduleId,
            tableModelId,
            params,
            record,
            isUpdate: true,
          };
          if (ReadComment) return;
          handleFlowComFn && handleFlowComFn(item);
        };
        return e;

      case BTN_EVENT_TYPE.IMPORT:
        e.onClick = function () {
          const item = {
            ...e,
            record,
          };
          importFile && importFile(item);
        };
        return e;
      case BTN_EVENT_TYPE.EXPORTFILE:
        e.onClick = function () {
          commonDownLoad({ ...e, record });
        };
        return e;

      case BTN_EVENT_TYPE.CUSTOMCOMPONENT:
        e.onClick = function () {
          handleCustomComponent &&
            handleCustomComponent({
              ...e,
              componentName: record.componentName || e.componentName,
              modelClassify: record.modelClassify || e.modelClassify,
              dataModelId: record.dataModelId || e.dataModelId,
              modelId: record.dataModelId || e.dataModelId,
              hanldRow: true,
              record,
              tabFn: tableFn(),
            });
        };
        return e;

      default:
        break;
    }

    return e;
  });

  return ret;
}
